package com.ocm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ocm.controller.util.OCMUtil;
import com.ocm.controller.validator.PropertyDefValidator;
import com.ocm.core.dto.PropertyDefinitionDto;
import com.ocm.core.service.PropertyDefService;

@Controller
public class ApplicationPropertyController {

   public static final String LIST_PAGE = "propertyDef/list";
   public static final String ADD_PAGE = "propertyDef/add";
   public static final String EDIT_PAGE = "propertyDef/edit";
   public static final String STATUS_PAGE = "propertyDef/status";
   
   private PropertyDefService propertyDefService;
   private PropertyDefValidator propertyDefValidator;
   
   @Autowired
   private ResourceBundleMessageSource messageSource;

   @Autowired
   public ApplicationPropertyController(
      @Qualifier("propertyDefService") PropertyDefService propertyDefService,
      @Qualifier("propertyDefValidator") PropertyDefValidator propertyDefValidator) {
      this.propertyDefService = propertyDefService;
      this.propertyDefValidator = propertyDefValidator;
   }

   @RequestMapping(value = "/propertyDef/{storageId}/list", method = RequestMethod.GET)
   public String listApplicationProperty(@PathVariable("storageId") long storageId, ModelMap model) {
      List<PropertyDefinitionDto> propertyDefDtos = this.propertyDefService.findAllFromStorageId(storageId);
      model.addAttribute("propertyDefs", propertyDefDtos);
      model.addAttribute("storageId", storageId);
      return LIST_PAGE;
   }

   @RequestMapping(value = "/propertyDef/{storageId}/add", method = RequestMethod.GET)
   public String viewAddPropertyDef(@PathVariable("storageId") long storageId, ModelMap model) {
      model.addAttribute("propertyTypes", OCMUtil.listPropertyTypes(messageSource));
      model.addAttribute("storageId", storageId);
      return ADD_PAGE;
   }

   @RequestMapping(value = "/propertyDef/{storageId}/add", method = RequestMethod.POST)
   public String viewAddPropertyDef(@PathVariable("storageId") long storageId, @ModelAttribute("propertyDef") PropertyDefinitionDto propertyDefDto, BindingResult result, ModelMap model) {
      this.propertyDefValidator.validatePropertyDef(storageId, propertyDefDto, result);
      if (!result.hasErrors()){
         propertyDefDto.setEditable(true);
         this.propertyDefService.save(propertyDefDto, storageId);
      }
      return STATUS_PAGE;
   }

   @RequestMapping(value = "/propertyDef/{storageId}/{propertyDefId}/edit", method = RequestMethod.GET)
   public String viewEditPropertyDef(@PathVariable("storageId") long storageId, @PathVariable("propertyDefId")long propertyId, ModelMap model) {
      PropertyDefinitionDto propertyDefDtos = this.propertyDefService.findId(propertyId);
      model.addAttribute("propertyDef", propertyDefDtos);
      model.addAttribute("propertyTypes", OCMUtil.listPropertyTypes(messageSource));
      return EDIT_PAGE;
   }

   @RequestMapping(value = "/propertyDef/{storageId}/{propertyDefId}/edit", method = RequestMethod.POST)
   public String viewEditPropertyDef(@PathVariable("storageId") long storageId, @PathVariable("propertyDefId")long propertyId, @ModelAttribute("propertyDef") PropertyDefinitionDto propertyDefDto, BindingResult result, ModelMap model) {
      this.propertyDefValidator.validate(propertyDefDto, result);
      if (!result.hasErrors()){
         this.propertyDefService.save(propertyDefDto, storageId);
      }
      return STATUS_PAGE;
   }
}
